package thread;

public class J_ThreadDaemon extends Thread {
    @Override
    public void run() {

        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getId()+" isDaemon "+Thread.currentThread().isDaemon()+" run thread " + i);
            try {
                sleep((int) (Math.random() * 1000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String args[]) {
        System.out.println("main start");
        J_ThreadDaemon t = new J_ThreadDaemon();
        t.setDaemon(true);
        t.start();
//        J_ThreadDaemon t1 = new J_ThreadDaemon();
//        t1.start();
        //所创建的后天USER线程结束后，后台线程才会被终止运行
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 3; i++) {
                    System.out.println(Thread.currentThread().getId()+" isDaemon "+Thread.currentThread().isDaemon()+" run thread " + i);
                    try {
                        sleep((int) (Math.random() * 1000));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        if (t.isDaemon()) {
            System.out.println("the thread is daemon thread");
        } else {
            System.out.println("the thread is not daemon thread");
        }
        System.out.println("main end");
    }
}
